package com.xian.controller;

import com.github.pagehelper.PageInfo;
import com.xian.pojo.Orders;
import com.xian.service.OrdersService;
import com.xian.utils.DateStringEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    @Qualifier("ordersServiceImpl")
    private OrdersService ordersService;
    //查询全部订单，为分页
//    @RequestMapping("/findAll.do")
//    public ModelAndView findAll(){
//        ModelAndView mv = new ModelAndView();
//        List<Orders> orders = ordersService.finaAll();
//        System.out.println(orders);
//
//        mv.addObject("ordersList",orders);
//        mv.setViewName("orders-list");
//        return mv;
//    }
    @RequestMapping("/findAll.do")
    @Secured("ROLE_ADMIN")
    public ModelAndView findAll(@RequestParam(name="page",required = true,
            defaultValue = "1") Integer page,@RequestParam(name="size",required = true,
            defaultValue = "4") Integer size){
        ModelAndView mv = new ModelAndView();
        List<Orders> orders = ordersService.finaAll(page, size);
        PageInfo pageInfo = new PageInfo(orders);
        //获取数据总条数
        long total = pageInfo.getTotal();
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("total",total);
        mv.setViewName("orders-page-list");
        return mv;
    }
//    @InitBinder
//    public void initBinder(WebDataBinder binder){
//        binder.registerCustomEditor(Date.class,new DateStringEditor());
//    }
    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam("id") String id){
        ModelAndView mv = new ModelAndView();
        Orders order = ordersService.findById(id);
        mv.addObject("orders",order);
        mv.setViewName("orders-show");
        return mv;
    }
}
